package simi;
import java.io.ByteArrayOutputStream;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.sshtools.j2ssh.SshClient;
public class SimiSessionListener implements HttpSessionListener {

	/* Session Creation Event */
	public void sessionCreated(HttpSessionEvent se) {

		// System.out.println("Simi Session was started...");
	}

	/* Session Invalidation Event */
	public void sessionDestroyed(HttpSessionEvent se) {

		// Here you want to do your cleanup stuff
		// System.out.println("Simi Session was destroyed, cleaning up...");
		HttpSession session = se.getSession();

		// Here we actually have to kill the threads running IOUtils
		ByteArrayOutputStream os = (ByteArrayOutputStream) session
				.getAttribute(Constants.RUNNING_OUTPUT_PARAM);
		SshClient ssh = (SshClient) session
				.getAttribute(Constants.SESSION);

		try {
			if (os != null)
				os.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		try {
			if (ssh != null && ssh.isConnected())
				ssh.disconnect();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		// System.out.println("Simi Session was destroyed, clean up
		// complete...");
	}
}